<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.4/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="layui-elem-field layuimini-search">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form-item" id="productForm" action="" lay-filter="productForm">
                    <div class="layui-form-item ">


                        <div class="layui-inline">
                            <label class="layui-form-label">产品类别</label>
                            <div class="layui-input-block" style="width:200px">
                                <input type="text" id="sname" name="sname" lay-verify="sname" autocomplete="off"
                                       placeholder=""
                                       class="layui-input">
                            </div>
                        </div>


                        <div class="layui-inline">
                            <label class="layui-form-label">产品名称</label>
                            <div class="layui-input-inline" style="width:200px">
                                <input type="text" id="pname" name="pname" lay-verify="pname" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <!--            <div class="layui-form-item">
                                        <label class="layui-form-label">产品类别</label>
                                        <div class="layui-input-block" style="width:200px" >
                                            <select name="sname" id="sname" lay-verify="sname" >
                                                <option value="">&#45;&#45;请选择&#45;&#45;</option>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="layui-form-item">
                                        <label class="layui-form-label">产品名称</label>
                                        <div class="layui-input-block" style="width:200px" >
                                            <select name="pname" id="pname" lay-verify="pname"  >
                                                <option value="" >&#45;&#45;请选择&#45;&#45;</option>
                                            </select>
                                        </div>
                                    </div>-->

                        <button type="button" id="formsubmit" class="layui-btn" lay-submit="" lay-filter="query">搜索
                        </button>
                    </div>
                </form>
                </form>
            </div>
        </fieldset>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs layui-btn-danger " lay-event="delete">在线</a>
        </script>

    </div>
</div>


<script src="../lib/layui-v2.5.4/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'layer', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            laydate = layui.laydate,
            table = layui.table;
        table.render({
            elem: '#currentTableId',
            url: '../api/sort/sortAndProductAllpage',
            request: {
                pageName: 'current' //页码的参数名称，默认：page
                , limitName: 'size' //每页数据量的参数名，默认：limit
            },
            cols: [[
                {field: 'pid', width: 210, title: '产品编号', sort: true, align: "center"},
                {field: 'pname', width: 210, title: '产品名称', align: "center"},
                {field: 'parea', width: 210, title: '产地', align: "center"},
                {field: 'sname', width: 210, title: '产品类别', align: "center"},
                {field: 'pml', width: 210, title: '产品规格', align: "center"},
                {title: '商品状态', minWidth: 50, templet: '#currentTableBar', fixed: "right", align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 5,
            page: true
        });


        $.get({
            url: "/api/sort/sortList", success: (resp) => {
                resp.forEach(r => {
                    console.log(r)
                    $("#sname").append("<option value='" + r.sid + "'>" + r.sname + "</option>");
                });
            }, dataType: "json"
        });
        $.get({
            url: "../api/product/productList", success: (resp) => {
                resp.forEach(r => {
                    console.log(r)
                    $("#pname").append("<option value='" + r.pid + "'>" + r.pname + "</option>");
                });
            }, dataType: "json"
        });

        function tableReload() {
            var sname = $("#sname").val()
            var pname = $("#pname").val()
            table.reload('currentTableId', {
                where: {
                    sname: sname,
                    pname: pname
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        }

        $("#formsubmit").click(function () {
            tableReload();
        });
        // 监听搜索操作
        /*form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
             layer.alert(result, {
                title: '最终的搜索信息'
            })

            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });
*/


        // 监听删除操作
        $(".data-delete-btn").on("click", function () {
            var checkStatus = table.checkStatus('currentTableId')
                , data = checkStatus.data;
            layer.alert(JSON.stringify(data));
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                layer.alert('编辑行：<br>' + JSON.stringify(data))
            } else if (obj.event === 'delete') {
                layer.confirm('确定要下架嘛', function (index) {
                    obj.del();
                    layer.close(index);
                });
            }
        });

    });
</script>


</body>
</html>